/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using CookComputing.XmlRpc;

namespace Whitemice.ZOGI
{
  public class Team : Entity, IZOGIStorable
  {
    protected IList<int>  memberIds = null;
  
    public Team() : base()
    {
    }
          
    public Team(IDictionary<string,object> x, int d)
      : base(x, d)
    {
    }
    
    internal override void Take(IDictionary<string,object> x, int d)
    {
      base.Take(x, d);
		  if (memberIds == null)
			  memberIds = new List<int>();      
			SetName(AsString(x,  "name"));
  	  if (x.ContainsKey("memberObjectIds"))
		 	{
        memberIds.Clear();
	    	foreach(int memberId in (x["memberObjectIds"] as Array))
		   	  memberIds.Add(memberId);
		  }
	  }
	  
	  public override bool FirstClass { get { return true; } }
	  
	  protected string _name;
	  protected void SetName(string n) { _name = n; }
	  public string Name { get { return _name; } }
	  
	  public IEnumerable MemberIds { get { return memberIds; } }
	  
	  public bool HasMember(int id)
	  {
	    return memberIds.Contains(id);
	  }
	  
	  public bool HasMember(Company e)
	  {
	    return memberIds.Contains(e.ObjectId);	  
	  }
	  
	  public void AddMember (int id)
	  {
	     if (HasMember(id)) return;
	     memberIds.Add(id); 
	  }
	  
	  public void AddMember (Company e)
	  {
	     if (HasMember(e)) return;
	       memberIds.Add(e.ObjectId);
	  }

    public void SetMembers(IEnumerable<int> members)
    {
      memberIds.Clear();
      foreach(int id in members)
        memberIds.Add(id);
    }

    public void SetMembers(IEnumerable<Entity> members)
    {
      memberIds.Clear();
      foreach(Entity entity in members)
        memberIds.Add(entity.ObjectId);
    }    
	  
	  public int MemberCount { get { return memberIds.Count; } }	  	  
	  
	  public override XmlRpcStruct Encode()
	  {
	    XmlRpcStruct rpcStruct;
	    ArrayList    members;
	    
	    rpcStruct = base.Encode();
	    members = new ArrayList();
	    foreach(int memberId in MemberIds)
	      members.Add(memberId);
	    
	    rpcStruct = new XmlRpcStruct();
	    rpcStruct.Add("entityName", "Team");
	    rpcStruct.Add("name", this.Name);
	    rpcStruct.Add("objectId", this.ObjectId);
	    rpcStruct.Add("objectVersion", this.Version);
	    rpcStruct.Add("memberObjectIds", members.ToArray());
	    
	    return rpcStruct;	 
	  } 
  }
}
